﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.OracleClient;
using System.Linq;
using System.Reflection;
using Dapper;
using Project.Common;
using Project.Data;
using Project.IData;
using Project.Model;
using Project.T4Code;
using System.Linq.Expressions;

namespace Project.ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            #region DataBase Init
            //MyDapper dapper = new MyDapper();
            //dapper.InitSql();

            Database.Init();
            Console.WriteLine("初始化结束！");
            #endregion

            #region T4
            T4ManagerCode t4Code = new T4ManagerCode();

            var types = t4Code.GetAssemblyTypes();
            string pro = t4Code.GetProvider();
            foreach (var type in types)
            {
                var p = t4Code.GetTypeProperties(type);

                //var x=  t4Code.GetConQueryStr(type.Name, p);
                //var y= t4Code.GetLoadSql(type.Name, p);

                //Console.WriteLine(t4Code.GetInsertSql(type.Name, p));

                var insert = SqlFactory.GetSqlBuilder(1, type.Name, p, pro);

                var load = SqlFactory.GetSqlBuilder(1, type.Name, p, pro);
                var n = load.GetSqlStr();
                var m = load.GetConQueryStr(type.Name, p);
                Console.WriteLine("-----------------------");
            }



            #endregion

            #region Test UserInfoData

            //UserInfo u = new UserInfo();

            ////u.Id = 1;
            //u.UserName = "bbb";
            //u.Age = 30;
            //u.Gender = Gender.男;
            //u.Address = "adf";
            //u.CreateTime = DateTime.Now;
            //u.Phone = "12222222222";
            //u.EmployeeId = 1;

            //UserInfoData userInfoData = new UserInfoData();
            ////int x = 0;
            ////for (int i = 0; i < 10000; i++)
            ////{
            ////    x += userInfoData.Create(u);
            ////}
            //int total = 0;
            ////Console.WriteLine(DateTime.Now);
            ////var y = userInfoData.LoadAllData();
            //Console.WriteLine(DateTime.Now);
            //var m = userInfoData.LoadByPage(1, 5, out total);
            //Console.WriteLine(DateTime.Now);
            //Console.WriteLine(total); 

            // UserInfoData userInfoData = new UserInfoData();

            //var user= userInfoData.LoadAllData(u => u.Id > 0);


            //IUserInfoData userInfoData = SimpleDataFactory.GetUserInfoData();
            //UserInfo userInfo = new UserInfo();
            //userInfo.Id = Guid.NewGuid().GetHashCode();
            //userInfo.UserName = "123";
            //userInfo.ActionInfoId = 1;
            //userInfo.Address = "123";
            //userInfo.Age = 18;
            //userInfo.CreateTime = DateTime.Now;
            //userInfo.EmployeeId = 1;
            //userInfo.Phone = "123123";
            //userInfo.Gender = Gender.男;
            //int i = 0;
            //Console.WriteLine(DateTime.Now);
            //while (i < 100000)
            //{
            //    var x = userInfoData.Create(userInfo);
            //    i++;
            //}
            //Console.WriteLine(DateTime.Now);
            //Console.WriteLine("添加完成");
            #endregion

            #region TestArray
            //List<string> list = new List<string>();
            //list.Add("Gender");

            //if (list.Contains("Gender"))
            //{
            //    Console.WriteLine(0);
            //}
            //else
            //{
            //    Console.WriteLine(1);
            //} 
            #endregion

            #region TestData
            //IEmployeeData employee = SimpleDataFactory.GetEmployeeData();
            //Employee e = new Employee();
            //e.LoginName = "admin";
            //e.LoginPwd = "123";
            //var x = employee.Create(e);


            //Employee e = new Employee();
            //e.LoginName = "123";
            //e.LoginPwd = "123";

            //using (var con = Database.GetConnection())
            //{
            //    var x = con.Insert<Employee>(e);

            //    Console.WriteLine(x);
            //}

            #endregion

            #region OracleTest

            //using (var con = Database.GetConnection())
            //{
            //    string sql = "select count(1) int from area";
            //    var x = con.Query<decimal>(sql);

            //    //var y = x.ToDictionary(A=>A);
            //    //var z = y["COUNT(1)"];
            //    //var Y = x.COUNT(1);
            //    //Console.WriteLine(x.COUNT(1));


            //    //string sql="insert into actionInfo values("

            //}
            //var x = GetReader<decimal>();
            //foreach (var i in x)
            //{
            //    Console.WriteLine(i);
            //}

            #endregion

            #region lambda Test

            //IUserInfoData userInfoData = new UserInfoData();
            //var userInfo = userInfoData.LoadAllData(u => u.Id > 0);

            //var x = userInfo.GroupBy(u => new { u.Id, u.EmployeeId })
            //    .Select(n => n.Select(m => new Employee()
            //    {
            //        Id = m.Employee.Id,
            //        LoginName = m.Employee.LoginName,
            //        LoginPwd = m.Employee.LoginPwd
            //    })).ToList();
            //var y = x[0].ToList();

            //var nn = (from u in userInfo
            //          group u by new
            //              {
            //                  u.Id,
            //                  u.UserName
            //              }
            //          into b
            //          select new UserInfo()
            //              {
            //                  Id = b.Select(z => z.Id),
            //                  UserName = b.Select(z => z.UserName)
            //              }).ToList();
            #endregion


            #region Testyield 迭代器

            //foreach (var item in Getyield(10))
            //{
            //    Console.WriteLine(string.Format("xxx{0}XXX",item));
            //}

            #endregion

            #region userData

            //UserInfoData userInfoData = new UserInfoData();
            //var total = 0;
            ////for (int i = 0; i < 50; i++)
            ////{
            ////    UserInfo u = new UserInfo();
            ////    u.UserInfoId = i+51;
            ////    u.UserName = "1213";
            ////    u.Phone = "1231231";
            ////    u.ActionInfoId = 1;
            ////    u.Address = "123";
            ////    u.Age = 18;
            ////    u.CreateTime = DateTime.Now;
            ////    u.EmployeeId = 1;
            ////    u.Gender = 0;
            ////    userInfoData.Create(u);
            ////}

            //var user = userInfoData.LoadModelByPage(1, 10, out total, u => u.UserInfoId != 0);
            //var x = user.ToList();
            #endregion

            #region TestOracleConnection
            //using (var connection = new OracleConnection("Password=dapper;Persist Security Info=True;User ID=dapper;Data Source=ORCL;"))
            //{
            //    connection.Open();
            //    using (var cmd = new OracleCommand("select * from userinfo", connection))
            //    {
            //        using (var reader = cmd.ExecuteReader())
            //        {
            //            while (reader.Read())
            //            {
            //                //UserInfo u=new UserInfo();
            //                //u.Id = Convert.ToInt32(reader.GetValue(0));
            //                //u.UserName = (string) reader.GetValue(1);
            //                //u.Age = (int) reader.GetValue(2);
            //                //u.Gender = (Gender) reader.GetValue(3);
            //                //u.Phone = (string) reader.GetValue(4);

            //                var x1 = (decimal)reader.GetValue(0);
            //                var x2 = (string)reader.GetValue(1);
            //                var x3 = (int)reader.GetValue(2);
            //                var x4 = (int)reader.GetValue(3);
            //                var x5 = (string)reader.GetValue(4);
            //                var x6 = (DateTime)reader.GetValue(5);
            //                var x7 = (string)reader.GetValue(6);
            //            }
            //        }
            //    }
            //} 
            #endregion

            #region DapperTestPage

            //string sql = "select UserInfoId , UserName, Age,  Gender, Address, CreateTime, Phone, EmployeeId eId, LoginName, LoginPwd, ActionInfoId aId, ActionName from (select row_number() over(order by u.UserInfoId desc) as rk, u.UserInfoId , u.UserName, u.Age, CAST(u.Gender as int) as Gender, u.Address, u.CreateTime, u.Phone, e.EmployeeId , e.LoginName, e.LoginPwd, a.ActionInfoId , a.ActionName from UserInfo u left join Employee e on u.EmployeeId = e.EmployeeId left join ActionInfo a on u.ActionInfoId = a.ActionInfoId) u where rk between 10 and 50  and u.UserInfoId>70";
            ////string sql = "select  u.UserInfoId , u.UserName, u.Age, CAST(u.Gender as int) as Gender, u.Address, u.CreateTime, u.Phone, e.EmployeeId , e.LoginName, e.LoginPwd, a.ActionInfoId , a.ActionName from UserInfo u left join Employee e on u.EmployeeId = e.EmployeeId left join ActionInfo a on u.ActionInfoId = a.ActionInfoId ";

            //using (var con = Database.GetConnection())
            //{
            //    var x = con.Query<UserInfo, Employee, ActionInfo, Tuple<UserInfo, Employee, ActionInfo>>(sql, (u, e, a) => Tuple.Create(u, e, a), splitOn: "eId,aId");
            //}

            #endregion


            Console.WriteLine("OK");
            Console.ReadKey();
        }


        public IEnumerable<UserInfo> LoadByPage(int pageIndex, int pageCount, out int total, Expression<Func<UserInfo, bool>> lambdaWhere)
        {

            var x = lambdaWhere.Body.ToString();

            //这里暂时是写的分页查询，这里还要继续修改，下面的linq分页不用了；如果使用T4模版，这里会被覆盖。。。
            string sql = "select sb.* from (select row_number() over(order by u.UserInfoId desc) as rk, u.UserInfoId as umId, u.UserName, u.Age, CAST(u.Gender as int) as Gender, u.Address, u.CreateTime, u.Phone, u.EmployeeId, u.ActionInfoId, e.EmployeeId eId, e.LoginName, e.LoginPwd, a.ActionInfoId aid, a.ActionName from UserInfo u left join Employee e on u.EmployeeId = e.EmployeeId left join ActionInfo a on u.ActionInfoId = a.ActionInfoId) sb where rk between 10 and 50  ";

            using (var connection = Database.GetConnection())
            {
                var userList = connection.Query<UserInfo, Employee, ActionInfo, UserInfo>(sql, (u, e, a) => { u.Employee = e; u.ActionInfo = a; return u; }, null, null, true, splitOn: "eId,aid");

                total = userList.Count();
                return userList;
            }
        }


        public static IEnumerable Getyield(int m)
        {
            for (int i = 0; i < m; i++)
            {
                yield return i;
            }
        }
        public static IEnumerable GetReader<T>()
        {
            string conStr = "Password=xrq;Persist Security Info=True;User ID=xrq;Data Source=ORCL;";
            using (var con = new OracleConnection(conStr))
            {
                con.Open();
                var cmd = new OracleCommand("select count(1) int from actioninfo", con);
                cmd.ExecuteScalar();
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        yield return (T)(reader.GetValue(0) == DBNull.Value ? 0 : reader.GetValue(0));
                    }
                }

            }
        }

    }
}
